//last updated 15-4

function onStart() {
	if (localStorage.getItem("noOfCases") == null) {
		localStorage.setItem("noOfCases", "0");
	}
	if (localStorage.getItem("activeCases") == null) {
		localStorage.setItem("activeCases", "[]");
	}
}

function addCase(caseObj) {
	var noOfCases = localStorage.getItem("noOfCases");
	noOfCases = parseInt(noOfCases) + 1;
	localStorage.setItem("noOfCases", noOfCases.toString());
	localStorage.setItem(noOfCases.toString(), JSON.stringify(caseObj));
	var activeCases = JSON.parse(localStorage.getItem("activeCases"));
	activeCases.push(noOfCases);
	localStorage.setItem("activeCases", JSON.stringify(activeCases));
	return noOfCases;

}

function getCase(caseId) {
	return JSON.parse(localStorage.getItem(caseId.toString()));
}

function updateCase(caseId, newCaseObj) {
	localStorage.setItem(caseId, JSON.stringify(newCaseObj));
}

function deleteCase(caseId) {
	localStorage.removeItem(caseId);
	var activeCases = JSON.parse(localStorage.getItem("activeCases"));
	for (var counter = 0; counter < activeCases.length; counter++) {
		if (activeCases[counter] == caseId) {
			activeCases.splice(counter, 1);
		}
	}
	localStorage.setItem("activeCases", JSON.stringify(activeCases));
}

function getDummyCaseObject() {
	return JSON
			.parse('{"caseRefNo":"","plaintiff":"","respondent":"","caseNote":"","noOfDates":"0","dates":[]}');
}

function addDate(caseId, caseDate, caseNote) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	var dateObj = JSON.parse('{"dateId":"","caseDate":"","caseNote":""}');
	var noOfDates = parseInt(caseObj.noOfDates) + 1;
	dateObj.dateId = noOfDates.toString();
	dateObj.caseNote = caseNote;
	dateObj.caseDate = caseDate;
	
	if(caseObj.dates.length==0){
	caseObj.dates.push(dateObj);
	}
	else{
	var i= caseObj.dates.length-1;
	var dates =caseObj.dates;
	alert(dates.length);
	
	while(caseDate<dates[i].caseDate && i>=0){ 
	alert(dates[i].caseDate);
		dates[i+1] = dates[i];
		i--;
	}
    dates[i+1] = dateObj;
	caseObj.dates=dates;
	}
	caseObj.noOfDates = noOfDates.toString();
	localStorage.setItem(caseId, JSON.stringify(caseObj));
}

function getDate(caseId, dateId) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
		if (dates[counter].dateId == dateId) {
			return dates[counter];
		}
	}
}

function updateDate(caseId, dateId, caseDate, caseNote) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	var dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
		if (dates[counter].dateId == dateId) {
			dates.splice(counter, 1);
			break;
		}
	}

	var dateObj = JSON.parse('{"dateId":"","caseDate":"","caseNote":""}');
	dateObj.dateId = dateId;
	dateObj.caseNote = caseNote;
	dateObj.caseDate = caseDate;
	
	caseObj.dates.push(dateObj);
	localStorage.setItem(caseId, JSON.stringify(caseObj));

}

function deleteDate(caseId, dateId) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	var dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
		if (dates[counter].dateId == dateId) {
			dates.splice(counter, 1);
			break;
		}
	}
	caseObj.dates = dates;
	localStorage.setItem(caseId, JSON.stringify(caseObj));
}

function getAllCases() {

}

function getAllDatesAscending() {

}
function getAllDatesDescending() {

}

function getCasesByDate() {

}

function search() {

}